//
//  WBHomeCell.swift
//  WB123
//
//  Created by xuyunde mac pro on 2017/7/4.
//  Copyright © 2017年 NSObjiect. All rights reserved.
//

import UIKit
import SnapKit
class WBHomeCell: UITableViewCell {
    
    // 记录底部视图的顶部约束 便于卸载和赋值
    var bottomViewTopConstraint: Constraint?


    //提供一个模型属性,供外界赋值
    var statusViewModel: WBStatusViewModel?{
        didSet{
            originalView.statusViewModel = statusViewModel
            
            // 卸载约束
            bottomViewTopConstraint?.deactivate()

            if let _ = statusViewModel?.homeModel?.retweeted_status{
                // 赋值
                retweetView.statusViewModel = statusViewModel
                // bottomView.top = retweetView.bottom
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                // 显示转发微博
                retweetView.isHidden = false
            }else{
                // 没有转发微博
                // ottomView.top =  originalView.bottom
                bottomView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })
                // 隐藏转发微博
                retweetView.isHidden = true
            }
            
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        //添加约束
        originalView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            //自己决定高度
//            make.height.equalTo(50)
            make.top.equalTo(8)
        }
        
        retweetView.snp.makeConstraints { (make) in
            // 它的这个约束应该是谁决定 -> 自己决定
//            make.height.equalTo(50)
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp.bottom)
        }

        
        bottomView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }

    }
    //懒加载控件
    /// 原创微博
    private lazy var originalView: WBHomeOriginalView = WBHomeOriginalView()
    // 转发微博
    private lazy var retweetView: WBHomeRetweetView = WBHomeRetweetView()
    /// 底部视图
    private lazy var bottomView: WBHomeBottomView = WBHomeBottomView()
    
}
